﻿Public Class NewKid
    Public eid As Integer
    Public cid As Integer
    Public sid As Int16

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        If txtname.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលឈ្មោះកូន")
            Exit Sub
        End If
        
        If cid = 0 Then
            cmdp.Parameters("@id") = cboemp.SelectedValue
            cmdp.Parameters("@name") = txtname.Text
            cmdp.Parameters("@dob") = dtpdob.Value
            cmdp.Parameters("@sex") = cbosex.SelectedValue
            cmdp.ExecuteNonQuery("INSERT INTO [Relative] (EmpId,RelateName,RelateDOB,TypeId,SexId,active) VALUES(@id,@name,@dob,2,@sex,1)")
            'MessageBox.Show("ADD")
            MessageBox.Show("បញ្ចូលបានសម្រេច")
        Else
            cmdp.Parameters("@name") = txtname.Text
            cmdp.Parameters("@dob") = dtpdob.Value
            cmdp.Parameters("@sex") = cbosex.SelectedValue
            cmdp.Parameters("@cid") = cid
            cmdp.ExecuteNonQuery("UPDATE [Relative] SET RelateName=@name,RelateDOB=@dob,SexId=@sex WHERE RelateId=@cid")
            'MessageBox.Show("UPD" & cid)
            MessageBox.Show("កែប្រែបានសម្រេច")
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub NewKid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadsex()
        loademp()
        If eid > 0 Then
            cboemp.SelectedValue = eid
            cboemp.Enabled = False
        End If

        If sid > 0 Then
            cbosex.SelectedValue = sid
        End If
    End Sub
    Private Sub loadsex()
        cbosex.ValueMember = "SexId"
        cbosex.DisplayMember = "SexName"
        cbosex.DataSource = cmdp.ExecuteDataTable("SELECT * FROM Sex")
    End Sub
    Private Sub loademp()
        cboemp.ValueMember = "EmpId"
        cboemp.DisplayMember = "EmpName"
        cboemp.DataSource = cmdp.ExecuteDataTable("SELECT EmpId,EmpName FROM Employee WHERE Active=true")
    End Sub
End Class